package org.lzy.shop.adapter

import android.content.Context
import android.view.ViewGroup
import com.chad.library.adapter4.BaseQuickAdapter
import com.chad.library.adapter4.viewholder.QuickViewHolder
import org.lzy.shop.R
import org.lzy.shop.cart.CartItem
import org.lzy.shop.util.setUrlRound

class SubmitOrderGoodsAdapter : BaseQuickAdapter<CartItem, QuickViewHolder>() {
    override fun onCreateViewHolder(
        context: Context,
        parent: ViewGroup,
        viewType: Int
    ): QuickViewHolder {
        return QuickViewHolder(R.layout.item_submit_order_goods, parent)
    }

    override fun onBindViewHolder(
        holder: QuickViewHolder,
        position: Int,
        item: CartItem?
    ) {
        item?.let {
            // 设置商品图片
            setUrlRound(it.goodsMainPic, 12f, holder.getView(R.id.iv_item_image))



            // 设置商品规格
            val specBuilder = StringBuilder()
            it.spec.forEachIndexed { index, spec ->
                specBuilder.append(spec.name)
                if (index < it.spec.size - 1) {
                    specBuilder.append(" ")
                }
            }
            // 设置商品名称
            holder.setText(R.id.tv_item_name, specBuilder.toString())

            // 设置商品价格
            // 假设spec不为空，取第一个规格的价格
            if (it.spec.isNotEmpty()) {
                holder.setText(R.id.tv_item_price, "￥${it.spec[0].price}.00")
            }

            // 设置商品数量，格式为" x 数量"
            holder.setText(R.id.tv_item_quantity, " x ${it.quantity}")


        }
    }
}